<?php

require_once('db.php');
require_once('/www/lib/php/KLogger.php');

//$log = new KLogger("log/phplog.txt", KLogger::DEBUG);
//$log->LogDebug("index.php called");

connect_db('junyi', 'admin123', 'bookmark');
$session = $_COOKIE['session'];

function format_one_bookmark($entry) {
  $row = sprintf("<div class='bookmark-entry'>
<div class='bookmark-content'>
<input name='bookmark-id' value='%s' type='hidden' />
<p class='title'>%s <span class='bm-author'>(by %s)</span></p> 
<p class='url'>
<a href='%s' class='url-anchor' target='_blank'>%s</a>
<span class='modified-date'>%s</span>
<img src='img/up.png' width='12'/>
<span class='rate'>%d</span>
<img src='img/down.png' width='12'/>
</p>
<p class='description'>%s</p> 
</div><img src='img/remove.ico' class='remove-icon' width='60' height='60'/>
</div>",
                 $entry['id'], $entry['title'], $entry['user'], $entry['url'], $entry['url'],
                 $entry['last_modified'], $entry['up'] - $entry['down'], $entry['text']);
  return $row;
}

function get_bookmarks() {
  global $session;
  if (isset($_GET['me'])) {
    $username = get_user_by_session($session);
    $sql = "select * from bookmark where user = '$username' order by up - down desc";
  } else if (isset($_GET['user'])) {
    $username = $_GET['user'];
    $sql = "select * from bookmark where user = '$username' order by up - down desc";
  } else {
    $sql = "select * from bookmark order by up - down desc";
  }
  $ret = mysql_query($sql);
  $output = "";
  while ($entry = mysql_fetch_assoc($ret)) {
    $output .= format_one_bookmark($entry);
  }
  return $output;
}

if (isset($_GET['get_private_bookmark'])) {
  $_GET['me'] = True;
  echo get_bookmarks();
  return;
}

if (isset($_GET['get_public_bookmark'])) {
  unset($_GET['me']);
  echo get_bookmarks();
  return;
}

?>

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

  <link rel="stylesheet" type="text/css" href="css/home.css" />
  <link rel="stylesheet" type="text/css" href="css/reset.css" />
  <link rel="stylesheet" type="text/css" href="css/modal.css" />
  
  <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
  <script type="text/javascript" src="js/jquery.easing.js"></script>
  <script type="text/javascript" src="js/jquery.cookie.js"></script>
  <script type="text/javascript" src="js/jquery.kwicks.js"></script>
  <script type="text/javascript" src="js/simplemodal.js"></script>
  <script type="text/javascript" src="js/home.js"></script>
  <script type="text/javascript" src="js/sprintf.js"></script>
  <title>Bookipedia | Bring order to the web, with your help</title>
</head>

<body>
  <div id="wrapper">
    <div id="header">
      <ul class="kwicks" >  
        <li id="kwick-public" class="green">
          <div class="kwick-inner">
            <img src="img/public.png" width="50" />
            <span class="menu-info">See & rate what everybody is visiting</span>
          </div>
        </li>  
        <li id="kwick-private" class="yellow">
          <div class="kwick-inner">
            <img src="img/private.png" width="50" />
            <span class="menu-info">Recover what you have marked</span>
          </div>
        </li>  
        <li id="kwick3" class="red"></li>  
        <li id="kwick4" class="purple"></li>  
        <li id="kwick-about" class="blue">
          <div class="kwick-inner">
            <img src="img/about.png" width="50" />
            <span class="menu-info">About this fancinating site and the fancinating coders</span>
          </div>
        </li>  
      </ul>  
    </div>
    <div id="add">
      <input type="text" name="newurl" id="newurl"/>
      <input type="image" src="img/plus.png"  id="add-new-btn" width="25"/>
      <img id="loading" src="img/hanoi.gif" width="200" />

      <div id="extra">
        <input type="text" name="title" id="new-title" />
        <textarea name="desc" id="new-desc" rows="10" cols="69"> </textarea>
        <input type="button" value="Add To DB" id="add-to-db" /> 
        <input type="button" value="Cancel" id="cancel-add-db" /> 
      </div>
    </div>

    <div id="bookmarks">
      <?= get_bookmarks()  ?>
    </div>
  </div>

  <div id="login">
    <div id="signin">
      <h3>LOGIN</h3>
      <ul>
        <li>
          <label>username:</label>
          <input id="username" type="text" name="username" />
        </li>
        <li>
          <label>password:</label>
          <input id="password" type="password" name="password" />
        </li>
        <li>
          <input type="checkbox" id="remember_me" name="rem" />
          <label id="rem-label">remember me</label>
          <img id="login-loading" src="img/loading_cute.gif" />
          <span id="login-info"></span>
        </li>
        <li>
          <button type="submit" id="login-btn">Login</button>
        </li>
      </ul>
    </div>
    <div id="signup">
      Not open for sign up. But you can wait...
    </div>
  </div>
</body>
</html>
